﻿using Cl.Model.DataBaseEntityDtos.User.User;
using Cl.Model.DataBaseEntityDtos.User.UserHistorys;
using Cl.Model.Results;
using Cl.Service.Service.User;
using Cl.Service.Service.User.UserHistorys;
using Cl.Service.Service.User.Users;
using Cl.Utils;
using Cl.WebApi.Controllers;
using Cl.WebApi.SwaggerModel;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Cl.WebApi.Areas.Admin.Users
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.AdminGroup)]
    //[Authorize]
    public class UserController : BaseController
    {
        #region 构造函数

        IUserServer _userService;
        IUserHistoryServer _userHistoryServer;


        /// <summary>
        /// 构造函数
        /// </summary>
        public UserController(IUserServer userService, IUserHistoryServer userHistoryServer)
        {
            _userService = userService;
            _userHistoryServer = userHistoryServer;
        }

        #endregion

        /// <summary>
        /// 获取单个用户详细信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public ResultInfoModel FindUserInfo(int UserId)
        {
            var result = _userService.FindUserInfo(UserId);
            return GetResult.Success("Success", result);
        }


        /// <summary>
        /// 查询用户列表数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public PageResultModel<UserOutDto> QueryUserData(SearchUserDto request)
        {
            var result = _userService.QueryUserData(request);
            return result;
        }

        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultInfoModel SaveUserData(UserInputDto inputDto)
        {

            inputDto.CreateId = GlobalUserManager.UserId;
            inputDto.CreateTime = DateTime.Now;
            var result = _userService.SaveUserData(inputDto);
            return result;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultInfoModel UpdateUserData(UserInputDto inputDto)
        {
            inputDto.CreateId = GlobalUserManager.UserId;
            inputDto.UpdateId = GlobalUserManager.UserId;
            inputDto.UpdateTime = DateTime.Now;
            var result = _userService.UpdateUserData(inputDto);
            return result;
        }


        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultInfoModel DeleteUserData(int userId)
        {

            var result = _userService.DeleteUserData(userId);
            return result;
        }

        /// <summary>
        /// 获取用户实体的修改记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public PageResultModel<UserHistoryOutDto> QueryUserHistoryData(SearchUserHistoryDto request)
        {
            var result = _userHistoryServer.QueryUserHistoryData(request);
            return result;
        }

    }
}
